#coding=utf-8

from django.db import models
from choices import ACTION_TYPES, PASS_A
from choices import SIZE_OPERATORS, GE_O
from choices import NAME_OPERATORS, KEYWORD_OPERATORS, EMATCH_O, HAS_WORD
from choices import KEYWORD_RANGE, ALL_R
from choices import CONDITION_TYPES, FROM_C

#===============================================================================
# ### condition ###
#===============================================================================
 
class Condition(models.Model):
    '''Specific condition type mapped to a base type'''
    
    ctype = models.IntegerField (choices=CONDITION_TYPES)
        
#===============================================================================
# ### conditions ###
#===============================================================================

class AttachmentCondition (models.Model):
    '''Attachment type, size and filename condition'''

    condition = models.OneToOneField(Condition, primary_key=True)
    
    size_operator = models.IntegerField(choices=SIZE_OPERATORS, default=GE_O, null=True, blank=True)
    size = models.IntegerField(null=True, blank=True)
    contentType = models.CharField(max_length=25, null=True, default="text", blank=True)
    contentSubtype = models.CharField(max_length=25, null=True, default="plain", blank=True)
    name_operator = models.IntegerField(choices=NAME_OPERATORS, default=EMATCH_O, null=True, blank=True)
    name = models.CharField(max_length=1000, null=True, blank=True)
    ext_operator = models.IntegerField(choices=NAME_OPERATORS, default=EMATCH_O, null=True, blank=True)
    ext = models.CharField(max_length=10, null=True, blank=True)
    how_many_operator = models.IntegerField(choices=SIZE_OPERATORS, default=GE_O)
    how_many = models.IntegerField(default=1)
        
class DateCondition (models.Model):
    '''Date, when e-mail was sent or received condition'''
    
    condition = models.OneToOneField(Condition, primary_key=True)
    
    from_d = models.DateTimeField()
    to_d = models.DateTimeField(null=True, blank=True)
      
class AddressCondition (models.Model):
    '''Sender of the e-mail condition'''
    
    condition = models.OneToOneField(Condition, primary_key=True)
    
    type = models.IntegerField (default=FROM_C, null=False, blank=True)
    local = models.CharField (max_length=1000, null=False, blank=False)
    domain = models.CharField (max_length=1000)
        
class KeywordCondition(models.Model):
    '''Keyword condition'''
    
    condition = models.OneToOneField(Condition, primary_key=True)
    
    keyword_operator = models.IntegerField(choices=KEYWORD_OPERATORS, default=HAS_WORD)
    keyword = models.CharField(max_length=1000)
    keyword_range = models.IntegerField(choices=KEYWORD_RANGE, default=ALL_R, null=True)
    how_many_operator = models.IntegerField(choices=SIZE_OPERATORS, default=GE_O)
    how_many = models.IntegerField(default=1)
          
class ANrCondition(models.Model):
    '''Number of attachments condition'''
    
    condition = models.OneToOneField(Condition, primary_key=True)
    
    anr_operator = models.IntegerField(choices=SIZE_OPERATORS, default=GE_O)
    anr = models.IntegerField()
      
class SizeCondition (models.Model):
    '''Message size condition'''

    condition = models.OneToOneField(Condition, primary_key=True)
    
    size_operator = models.IntegerField(choices=SIZE_OPERATORS, default=GE_O)
    size = models.IntegerField()
      
class ResentCondition(models.Model):
    '''Was message resent condition'''
    
    condition = models.OneToOneField(Condition, primary_key=True)
    
    resent = models.BooleanField()
                    
#===============================================================================
# ### rule ###
#===============================================================================

class Rule(models.Model):
    '''Filtering rule model'''
    
    PR_CHOICES = ((1, '1 (min)'),
                  (2, '2'),
                  (3, '3'),
                  (4, '4'),
                  (5, '5 (max)'))
    
    action = models.IntegerField (choices=ACTION_TYPES, default=PASS_A, null=False)
    priority = models.IntegerField(choices=PR_CHOICES, default=5, null=False)
    is_tmp = models.BooleanField(default = False, null=False, blank = True, db_index=True)
    conditions = models.ManyToManyField (Condition)
    expression = models.CharField (max_length=1000, null=False, blank=False)
    
